﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Configuration;
using Pub.Controls;
using Pub.Lib;
using Pub.Tools;
using Pub;
using System.IO;

namespace MyAPP
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private     DataTable dt;
        private string strSQL=string.Empty;

        object objApp;
        object objBook;
        object objBooks;
        object objSheets;
        object objSheet;
        object objCells;

        private void btndt_Click(object sender, EventArgs e)
        {
          strSQL = textBox1.Text;
          DateTime dt1 = DateTime.Now;


          dt = Helper.DBHelper.GetDataTableOld(strSQL, PubVar.ConnMain);
          DateTime dt2 = DateTime.Now;

          bindingSource1.DataSource = dt;

          dataGridView1.DataSource = bindingSource1;
          TimeSpan ts = dt2 - dt1;


          string strMess = "读取成功.记录数:" + dt.Rows.Count.ToString() + ",花费时间:" + ts.TotalSeconds.ToString("n3") + "秒";

         MessageBox.Show(strMess);
            
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;

        //    this.dataGridView1.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter; 
            
            //Pub.strDataAccessType = ConfigurationManager.AppSettings["DataAccessType"];

            //if (Pub.strDataAccessType.ToUpper() == "Remoting".ToUpper())
            //{
            //    RemotingDB.iniobj();
            //}
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
       //      string  strSQL = "select * from dbo.DW_Develop where cDevelopCode like '0%'";
             DateTime dt1 = DateTime.Now;
            DataTable dd=dt.GetChanges();
            int ii = 0;
            if (dd != null)
            {
                ii = dd.Rows.Count;
            }
            // int ii = dd.Rows.Count;
          //  Boolean bl = DbHelper.SaveDataTable(strSQL, dt.GetChanges(), PubVar.ConnMain);
            Boolean bl = Helper.DBHelper.SaveDataTable(strSQL, dt.GetChanges(), PubVar.ConnMain, "EA_CITYNum", PubVar.SysDAT);

             DateTime dt2 = DateTime.Now;

             TimeSpan ts = dt2 - dt1;

             string strMess = string.Empty;
             if (bl)
             {
               //  int iii = bindingSource1.Position;
                 dt.AcceptChanges();
                 strMess = "保存成功.记录数:" + ii.ToString() + ",花费时间:" + ts.TotalSeconds.ToString("n3") + "秒";
                 //strSQL = textBox1.Text;
                 //dt = DbHelper.GetDataTable(strSQL);

                 //bindingSource1.DataSource = dt;

                 //dataGridView1.DataSource = bindingSource1;
             //    bindingSource1.Position = iii;
                 
             }
             else
             {
                 strMess = "保存失败.记录数:" + ii.ToString() + ",花费时间:" + ts.TotalSeconds.ToString("n3") + "秒"; ;
             }
             MessageBox.Show(strMess);
                 //+ "秒.\r\n长度" + dd.ToString("n1") + "K,下载速度:" + ddk.ToString("n1") + "K/秒";
            
        }

        private void btndeve_Click(object sender, EventArgs e)
        {
            textBox1.Text = "select * from dbo.DW_Develop WHERE (1=1)";            
        }

        private void btnLx_Click(object sender, EventArgs e)
        {
            textBox1.Text = "SELECT     EA_City.CityID, EA_City.CityName, EA_City.ZipCode, EA_City.ProvinceID, EA_CITYNum.fNum \r\n" +
"FROM         EA_City LEFT OUTER JOIN \r\n" +
"                      EA_CITYNum ON EA_City.CityID = EA_CITYNum.CityID WHERE (1=1)";
        }

        private void btnEs_flow_Click(object sender, EventArgs e)
        {
            textBox1.Text = "SELECT FlowID,FlowModelID,AppID,Name,Subject,PreMessageID,PreFlowID,JoinType,StartTime,EndTime,ExpectEndTime,Status,Attachment,StarterID  FROM Es_Flow WHERE (1=1)";
        }

        private void btnXML_Click(object sender, EventArgs e)
        {
            strSQL = textBox1.Text;
            DateTime dt1 = DateTime.Now;


            dt = Helper.DBHelper.GetDataTableXML(strSQL, PubVar.ConnMain);
            DateTime dt2 = DateTime.Now;

            bindingSource1.DataSource = dt;

            dataGridView1.DataSource = bindingSource1;
            TimeSpan ts = dt2 - dt1;


            string strMess = "读取成功.记录数:" + dt.Rows.Count.ToString() + ",花费时间:" + ts.TotalSeconds.ToString("n3") + "秒";

            MessageBox.Show(strMess);
        }



        private void btnXMLZip_Click(object sender, EventArgs e)
        {
            strSQL = textBox1.Text;
            DateTime dt1 = DateTime.Now;


            dt = Helper.DBHelper.GetDataTableXMLZip(strSQL, PubVar.ConnMain);
            DateTime dt2 = DateTime.Now;

            bindingSource1.DataSource = dt;

            dataGridView1.DataSource = bindingSource1;
            TimeSpan ts = dt2 - dt1;


            string strMess = "读取成功.记录数:" + dt.Rows.Count.ToString() + ",花费时间:" + ts.TotalSeconds.ToString("n3") + "秒";

            MessageBox.Show(strMess);

            //GZipEncoder.GZipMessageEncodingBindingElementImporter
            
        }


        private void btndelete_Click(object sender, EventArgs e)
        {
           // MessageBox.Show(dataGridView1.CurrentCell.RowIndex.ToString());

            //dt.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
            dt.Rows[dataGridView1.CurrentCell.RowIndex].Delete();
            //btnsave_Click(null, null);
        }



        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {
            if (dt != null)
            {
                if (dt.GetChanges() != null)
                {
                    //      btnsave_Click(null,null);
                }
            }
        }

        private void btndsser_Click(object sender, EventArgs e)
        {
            strSQL = textBox1.Text;
            DateTime dt1 = DateTime.Now;


            dt = Helper.DBHelper.GetDataTableSerialize(strSQL, PubVar.ConnMain);
            DateTime dt2 = DateTime.Now;

            bindingSource1.DataSource = dt;

            dataGridView1.DataSource = bindingSource1;
            TimeSpan ts = dt2 - dt1;


            string strMess = "读取成功.记录数:" + dt.Rows.Count.ToString() + ",花费时间:" + ts.TotalSeconds.ToString("n3") + "秒";

            MessageBox.Show(strMess);
        }

        private void btndsserzip_Click(object sender, EventArgs e)
        {
            strSQL = textBox1.Text;
            DateTime dt1 = DateTime.Now;


            dt = Helper.DBHelper.GetDataTableSeriesZip(strSQL, PubVar.ConnMain);
            DateTime dt2 = DateTime.Now;

            bindingSource1.DataSource = dt;

            dataGridView1.DataSource = bindingSource1;
            TimeSpan ts = dt2 - dt1;


            string strMess = "读取成功.记录数:" + dt.Rows.Count.ToString() + ",花费时间:" + ts.TotalSeconds.ToString("n3") + "秒";

            MessageBox.Show(strMess);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strSql = textBox1.Text;
            string strdbname = Pub.Tools.DBClass.GetDBName(strSql);

            string strdbfields = Pub.Tools.DBClass.GetFieldsName(strSql, strdbname, Helper.DBHelper.GetDataTable(strSql));

            Pub.Tools.MsgTools.InfoMsg(strdbname);
            Pub.Tools.MsgTools.InfoMsg(strdbfields);
            
        }

        private void btnOtherDb_Click(object sender, EventArgs e)
        {
            textBox1.Text = "select * from DW_OA_New..DW_Develop WHERE (1=1)";

            //throw new Exception("自定义异常");
        }

        private void btnDTLoad_Click(object sender, EventArgs e)
        {
            //string fax = "来自" + MyPub.SysClass.GetIPAddress() + "客户端的传真:aaa";
            
            //PubVar.objsysUser.SendFax( MyPub.SysClass.GetIPAddress() ,fax);

            //dt = DbHelper.GetDataTableXML(strSQL, PubVar.ConnMain);
            //dt.WriteXmlSchema(@"c:\1.xml");

        //    MessageBox.Show(PubVar.WcfProxy.Connstring(PubVar.ConnMain));

       //     Boolean bl = MyPub.ADClass.Authenticate("dw", "suzhibai", "xm123321");

      //      MyPub.ADClass.ChangeWinUserPasswd("suzhibai", "1", "xm123321");

            dt1.Text = textBox1.Text;
            dt1.DataBinds();
            DataTable dd = dt1.DataTable;
            bindingSource1.DataSource = dt1.DataTable;

            dataGridView1.DataSource = bindingSource1;
      //      MyPub.MsgClass.InfoMsg("DD");
            
        }


        private void dt1_OnNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["fNum"] = 10;
        }

        private void dt1_OnRow_Changing(object sender, DataRowChangeEventArgs e)
        {
          //  MyPub.MsgClass.InfoMsg("1111");
          
        }

        private void dt1_OnColumn_Changing(object sender, DataColumnChangeEventArgs e)
        {

        }

        private void bindingSource1_DataMemberChanged(object sender, EventArgs e)
        {

        }

        private void bindingSource1_DataSourceChanged(object sender, EventArgs e)
        {
        
        }

        private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
       
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            //if

            //MyPub.MsgClass.InfoMsg("2222");
        }

        private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
        {

            

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dt1_OnRow_Changed(object sender, DataRowChangeEventArgs e)
        {
            
        }

        private void btnDTSave_Click(object sender, EventArgs e)
        {
            Boolean bl = dt1.Save;
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string ss;
           //ExcelClass.GenerateExcel(@"c:\2.xlsx",dt1.GetDT,out ss);
        }

        private void btnexcelopen_Click(object sender, EventArgs e)
        {
            string errorinfo = string.Empty;
            //ExcelClass.OpenExcel(@"c:\2.xlsx", ref objApp, ref objBook, ref objSheet, ref objCells, ref errorinfo);

            //ExcelClass.WriteValue(ref objSheet,ref objCells,3,5,"11111",ref errorinfo);

            //string ss = ExcelClass.ReadValue(ref objSheet, ref objCells, 7, 3,  ref errorinfo); 

            //ExcelClass.SaveExcel(@"c:\2.xlsx", ref objApp, ref objBook, ref errorinfo);
        }

        private void btnsetvalue_Click(object sender, EventArgs e)
        {
            IntPtr pt;
            string fn = @"c:\2.xlsx";
            Pub.Tools.ExcelApp excelapp = new Pub.Tools.ExcelApp();
            pt = excelapp.pt;
            excelapp.Visible = true;
            excelapp.SafeOpen(fn);
            String c7 = excelapp.GetValue(30,30);
            excelapp.SetNumberFormatLocal("A2:A10", "@");
            excelapp.SetValue("A2", "1234567896");

            string abc = "张三\t" + "李四\t" + "王五";

          //  excelapp.Copy("A9,G9");

         //   excelapp.Paste("A23");

         ////   excelapp.ActiveSheet(2);
         //   excelapp.Paste(3, 5,abc);
         //   excelapp.SetMerge("E9,G9");

            excelapp.InsertColumns(1);
         //   excelapp.DeleteRows(9);
        //    excelapp.Preview();
           //int? ii= excelapp.getMaxRows();
            //MessageBox.Show(c7);
            excelapp.SetValue("C9", "张三");
            excelapp.Save(fn);
            excelapp.Dispose();
        }

        private void btnexcelsave_Click(object sender, EventArgs e)
        {

        }

 



        private void btnallfile_Click(object sender, EventArgs e)
        {
            List<string> ListFiles = new List<string>();//初始化 

            Pub.Tools.SysTools.GetPathAllFiles(ListFiles, @"C:\Users\suzhibai.DW\Documents", "xls");
            for (int i = 0; i < ListFiles.Count; i++)
            {
                string a = ListFiles[i];
                MessageBox.Show(a);
            }



        }

        private void button4_Click(object sender, EventArgs e)
        {
            
            string aa = Pub.Tools.StringTools.X2X("ZZZZZ", 36, 10);
            MessageBox.Show(aa);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }



    }
}
